home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / gtalk / gtalkStream.pyo (.txt) < prev   
Python Compiled Bytecode  |  2008-10-13  |  4KB  |  108 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from pyxmpp import sasl
  5. from pyxmpp.exceptions import SASLNotAvailable, SASLMechanismNotAvailable, SASLAuthenticationFailed, FatalStreamError
  6. from jabber.threadstream import ThreadStream
  7. from pyxmpp.streamsasl import SASL_NS
  8. from pyxmpp.streambase import STREAM_NS
  9. from pyxmpp.jid import JID
  10. import libxml2
  11. from logging import getLogger
  12. log = getLogger('gtalkStream')
  13. GTALK_AUTH_NS = 'http://www.google.com/talk/protocol/auth'
  14.  
  15. class GoogleTalkStream(ThreadStream):
  16.     
  17.     def stream_start(self, doc):
  18.         self.doc_in = doc
  19.         log.debug('input document: %r' % (self.doc_in.serialize(),))
  20.         
  21.         try:
  22.             r = self.doc_in.getRootElement()
  23.             if r.ns().getContent() != STREAM_NS:
  24.                 self._send_stream_error('invalid-namespace')
  25.                 raise FatalStreamError, 'Invalid namespace.'
  26.         except libxml2.treeError:
  27.             self._send_stream_error('invalid-namespace')
  28.             raise FatalStreamError, "Couldn't get the namespace."
  29.  
  30.         self.version = r.prop('version')
  31.         if self.version and self.version != '1.0':
  32.             self._send_stream_error('unsupported-version')
  33.             raise FatalStreamError, 'Unsupported protocol version.'
  34.         
  35.         if self.initiator:
  36.             self.stream_id = r.prop('id')
  37.             peer = r.prop('from')
  38.             if peer:
  39.                 peer = JID(peer)
  40.                 self.peer = peer
  41.             
  42.         else:
  43.             to = r.prop('to')
  44.             if to:
  45.                 to = self.check_to(to)
  46.                 if not to:
  47.                     self._send_stream_error('host-unknown')
  48.                     raise FatalStreamError, 'Bad "to"'
  49.                 
  50.                 self.me = JID(to)
  51.             
  52.             self._send_stream_start(self.generate_id())
  53.             self._send_stream_features()
  54.             self.state_change('fully connected', self.peer)
  55.             self._post_connect()
  56.         if not self.version:
  57.             self.state_change('fully connected', self.peer)
  58.             self._post_connect()
  59.         
  60.  
  61.     
  62.     def _sasl_authenticate(self, username, authzid, mechanism = None):
  63.         if not self.initiator:
  64.             raise SASLAuthenticationFailed, 'Only initiating entity start SASL authentication'
  65.         
  66.         while not self.features:
  67.             self._GoogleTalkStream__logger.debug('Waiting for features')
  68.             self._read()
  69.         if not self.peer_sasl_mechanisms:
  70.             raise SASLNotAvailable, "Peer doesn't support SASL"
  71.         
  72.         if not mechanism:
  73.             mechanism = None
  74.             for m in self.sasl_mechanisms:
  75.                 if m in self.peer_sasl_mechanisms:
  76.                     mechanism = m
  77.                     break
  78.                     continue
  79.             
  80.             if not mechanism:
  81.                 raise SASLMechanismNotAvailable, "Peer doesn't support any of our SASL mechanisms"
  82.             
  83.             self._GoogleTalkStream__logger.debug('Our mechanism: %r' % (mechanism,))
  84.         elif mechanism not in self.peer_sasl_mechanisms:
  85.             raise SASLMechanismNotAvailable, '%s is not available' % (mechanism,)
  86.         
  87.         self.auth_method_used = 'sasl:' + mechanism
  88.         self.authenticator = sasl.client_authenticator_factory(mechanism, self)
  89.         initial_response = self.authenticator.start(username, authzid)
  90.         if not isinstance(initial_response, sasl.Response):
  91.             raise SASLAuthenticationFailed, 'SASL initiation failed'
  92.         
  93.         root = self.doc_out.getRootElement()
  94.         xmlnode = root.newChild(None, 'auth', None)
  95.         ns = xmlnode.newNs(SASL_NS, None)
  96.         xmlnode.setNs(ns)
  97.         xmlnode.setProp('mechanism', mechanism)
  98.         ga = xmlnode.newNs(GTALK_AUTH_NS, 'ga')
  99.         xmlnode.newNsProp(ga, 'client-uses-full-bind-result', 'true')
  100.         if initial_response.data:
  101.             xmlnode.setContent(initial_response.base64())
  102.         
  103.         self._write_raw(xmlnode.serialize(encoding = 'UTF-8'))
  104.         xmlnode.unlinkNode()
  105.         xmlnode.freeNode()
  106.  
  107.  
  108.